package it.koutifaris.travianmap.tile.tilemap;

import static it.koutifaris.testforclassutils.IsTestForAssert.assertIsTestFor;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import it.koutifaris.travianmap.model.Village;
import it.koutifaris.travianmap.tile.Tile;
import it.koutifaris.travianmap.tile.TileBounds;
import it.koutifaris.travianmap.tile.bounds.DefaultTileBounds;

import java.util.ArrayList;

import junit.framework.Assert;
import android.test.AndroidTestCase;

public class TileCacheImplTest extends AndroidTestCase {

	private TileCacheImpl tileCache;

	@Override
	public void setUp() {
		tileCache = new TileCacheImpl();
	}

	public void testPkgAndName() {
		assertIsTestFor(TileCacheImpl.class);
	}

	public void testElementInCache() {
		TileBounds tileBounds = getTileBounds();

		Tile tile = getTileMockup(tileBounds);
		tileCache.add(tile);

		Assert.assertTrue(tileCache.isInCache(-2, -1));
	}

	public void testElementRemovedFromCache() {
		TileBounds tileBounds = getTileBounds();

		Tile tile = getTileMockup(tileBounds);
		tileCache.add(tile);
		tileCache.remove(tile);

		Assert.assertFalse(tileCache.isInCache(-2, -1));
	}

	private Tile getTileMockup(TileBounds tileBounds) {
		Tile tile = mock(Tile.class);
		when(tile.getBounds()).thenReturn(tileBounds);
		when(tile.getVillages()).thenReturn(new ArrayList<Village>());
		when(tile.getNumberOfVillages()).thenReturn(0);

		return tile;
	}

	private TileBounds getTileBounds() {
		return DefaultTileBounds.builder()
				.setOrigin(-3, -3)
				.withSize(3, 3)
				.build();
	}
}
